<?php
/**
 * Front controller for Yii Minify
**/
Yii::import('application.vendors.Zend.Oauth.Consumer',true);
Yii::import('application.vendors.Zend.Gdata.Calendar',true);
Yii::import('application.vendors.Zend.Gdata.Calendar.EventQuery',true);
Yii::import('application.vendors.Zend.Http.Client.Adapter.Socket',true);
class OpenidController extends Controller
{
	public $provider = array('Google'=>'https://www.google.com/accounts/o8/id');
	
	public function actionRequest() {
		$provider = 'spartanlogistics.biz';//$this->returnProvider();
		Yii::app()->user->setState('googleapp',true);
		$openid = new OpenIDIndentity($provider);
		$redirectURL = $openid->authenticate();
		if($redirectURL != false) {
			$this->redirect($redirectURL);
		}
		else
			throw new CHttpException(406,'Unable to perfor your authorize at the moment.');
	}
	public function actionReturn() {
		$provider = $this->returnProvider();
		$openid = new OpenIDIndentity($provider);
		if($openid->checkAutheticate()){
			$this->redirect(Yii::app()->user->getReturnUrl());
		}
	}
	private function returnProvider(){
		if(isset($_GET['domain'])){
			$provider = $_GET['domain'];
			Yii::app()->user->setState('googleapp',true);
			return $provider;
		}
		else {
			if(isset($_GET['provider']))
				$provider = $_GET['provider'];
			if(!isset($this->provider[$provider]))
				$provider = 'Google';
			return $this->provider[$provider];
		}
	}
	
	public function actiongetFeed(){
		$options = array(
    'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
    'version' => '1.0',
    'signatureMethod' => 'HMAC-SHA1',
    'consumerKey' => '708142073990.apps.googleusercontent.com',
    'consumerSecret' => '6NUheKGdmyg8IKURGvA/OpHF'
);

/**
 * Create HTTP Client object which adds OAuth Authorization
 * headers to outbound requests.
 */
$consumer = new Zend_Oauth_Consumer($options);
$token = new Zend_Oauth_Token_Access();
$httpClient = $token->getHttpClient($options);
$projection = 'basic'; 
$singleevents = 'true';
 $maxResults = 1;
 $calendarClient = new Zend_Gdata_Calendar($httpClient);
  $calendarEventQuery = new Zend_Gdata_Calendar_EventQuery(
      'https://www.google.com/calendar/feeds');
  $calendarEventQuery->setVisibility('private');
  $calendarEventQuery->setProjection($projection);
  $calendarEventQuery->setParam('singleevents', $singleevents);
  $calendarEventQuery->setParam('sortorder', 'ascending');
  $calendarEventQuery->setParam('orderby', 'starttime');
  $calendarEventQuery->setParam('start-min', date('c'));
  $calendarEventQuery->setParam('max-results', $maxResults);
  $calendarEventQuery->setParam('xoauth_requestor_id', 'chris.sim@spartanlogistics.biz');
  $calendarEventsFeed = $calendarClient->getCalendarEventFeed($calendarEventQuery);
  if (count($calendarEventsFeed->getEntry()) == 1) { 
    $calendarEventEntries = $calendarEventsFeed->getEntry();
    $calendarEventEntry = $calendarEventEntries[0];
    $summary = 'Title: ' . $calendarEventEntry->title->text . "<br >\n" .$calendarevententry->summary->text;
  }
  else
  	echo "error";
  echo $summary;
}
}
